package com.android.infantschool.ui.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.infantschool.R;
import com.android.infantschool.bean.BaseBean;
import com.android.infantschool.bean.ClockingLeaveBean;
import com.android.infantschool.bean.ResponseBean;
import com.android.infantschool.model.config.AppConfig;
import com.android.infantschool.model.config.AppDataSource;
import com.android.infantschool.model.mvp.presenter.ClockLeaveApprovePresenter;
import com.android.infantschool.model.mvp.presenter.ClockLeaveApprovePresenterImpl;
import com.android.infantschool.model.mvp.presenter.ClockLeavePresenter;
import com.android.infantschool.model.mvp.presenter.ClockLeavePresenterImpl;
import com.android.infantschool.model.mvp.view.ClockLeaveApproveView;
import com.android.infantschool.model.mvp.view.ClockLeaveView;
import com.android.infantschool.ui.adapter.ClockingLeaveAdapter;
import com.android.infantschool.ui.dialog.CommomDialog;
import com.android.infantschool.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Administrator
 * @particulars
 * @time 2019\7\3 0003 17:21
 * @class describe 选项卡显示请假模块
 */
public class ClockingLeaveFragment extends BaseFragment implements ClockingLeaveAdapter.OnLister, ClockLeaveView, ClockLeaveApproveView {

    private RecyclerView recyclerView;
    private ClockingLeaveAdapter leaveAdapter;
    private ClockLeavePresenter leavePresenter = null;
    private ClockLeaveApprovePresenter approvePresenter = null;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chocking_leave, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        leavePresenter = new ClockLeavePresenterImpl(this);
        approvePresenter = new ClockLeaveApprovePresenterImpl(this);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        leaveAdapter = new ClockingLeaveAdapter(activity, this);
        recyclerView.setAdapter(leaveAdapter);

        this.showLoading();
        Map<String, String> map = new HashMap<>();
        map.put("token", AppConfig.getToken());
        leavePresenter.getClockLeave(map);
    }

    @Override
    public void errorClockLeaveApprove(String error) {
        ClockingLeaveFragment.this.dismissLoading();
        ToastUtil.showShortToast(activity, "网络异常:" + error);
    }

    @Override
    public void successClockLeaveApprove(BaseBean baseBean) {
        ClockingLeaveFragment.this.dismissLoading();
        ResponseBean responseBean = (ResponseBean) baseBean;
        if (responseBean.getCode() == 0) {
            this.showLoading();
            Map<String, String> map = new HashMap<>();
            map.put("token", AppConfig.getToken());
            leavePresenter.getClockLeave(map);
        } else {
            ToastUtil.showShortToast(activity, "审批失败:" + responseBean.getMsg());
        }
    }

    @Override
    public void errorClockLeave(String error) {
        this.dismissLoading();
        leaveAdapter.clear();
        ToastUtil.showShortToast(activity, "网络异常:" + error);
    }

    @Override
    public void successClockLeave(BaseBean baseBean) {
        this.dismissLoading();
        ClockingLeaveBean leaveBean = (ClockingLeaveBean) baseBean;
        if (leaveBean.getCode() == 0) {
            leaveAdapter.setData(leaveBean.getData());
        } else {
            leaveAdapter.clear();
            ToastUtil.showShortToast(activity, "请求失败:" + leaveBean.getMsg());
        }
    }

    @Override
    public void lister(final int position) {
        if (leaveAdapter.list.get(position).getIs_pass() != 1) {
            new CommomDialog(activity, R.style.dialog_loading, "进行审批", new CommomDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean confirm) {
                    if (confirm) {
                        dialog.dismiss();
                        ClockingLeaveFragment.this.showLoading();
                        Map<String, String> map = new HashMap<>();
                        map.put("token", AppConfig.getToken());
                        map.put("id", String.valueOf(leaveAdapter.list.get(position).getId()));
                        Log.e(TAG, "map:" + map.toString());
                        approvePresenter.postClockLeaveApprove(map);
                    }
                }
            }).setTitle("审批").show();
        } else {
            ToastUtil.showShortToast(activity, "您已审批通过");
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (leavePresenter != null) {
            leavePresenter.onDestroy();
            leavePresenter = null;
        }
        if (approvePresenter != null) {
            approvePresenter.onDestroy();
            approvePresenter = null;
        }
    }
}
